package com.threeox.commonlibrary.adapter.navigation;

import android.view.View;

import com.threeox.commonlibrary.adapter.base.AutoIntoValAdapter;
import com.threeox.commonlibrary.adapter.base.BaseViewHolder;
import com.threeox.commonlibrary.inter.navigation.INavigationAdapter;
import com.threeox.commonlibrary.inter.navigation.INavigationMsg;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;

/**
 * 
 * @ClassName: CommonNavigationAdapter
 * 
 * @Description: TODO()
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/8/9 14:23
 * 
 * @version 1.0
 * 
 */
public class CommonNavigationAdapter<T> extends AutoIntoValAdapter<T> implements INavigationAdapter {

    private Integer mItemFirstViewId;

    @Override
    public void conver(BaseViewHolder holder, int position, int viewType, T data) {
        super.conver(holder, position, viewType, data);
        setFirstView(holder, position);
    }

    /**
     * 设置是否显示第一次出现的View
     *
     * @param holder
     * @param position
     */
    protected void setFirstView(BaseViewHolder holder, int position) {
        if (null != mItemFirstViewId) {
            View view = holder.getView(mItemFirstViewId.intValue());
            // 根据position获取分类的首字母的Char ascii值
            int section = getSectionForPosition(position);
            // 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            if (position == getPositionForSection(section)) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     *
     * @param position
     * @return
     */
    @Override
    public int getSectionForPosition(int position) {
        try {
            String navigationValue = getNavigationValue(position);
            if (EmptyUtils.isNotEmpty(navigationValue)) {
                return navigationValue.charAt(0);
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "getSectionForPosition:报错了:" + e.getMessage());
        }
        return -1;
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     *
     * @param sectionIndex
     * @return
     */
    @Override
    public int getPositionForSection(int sectionIndex) {
        try {
            for (int i = 0; i < getCount(); i++) {
                String navigationValue = getNavigationValue(i);
                if (EmptyUtils.isNotEmpty(navigationValue)) {
                    char firstChar = navigationValue.toUpperCase().charAt(0);
                    if (firstChar == sectionIndex) {
                        return i;
                    }
                }
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "getPositionForSection:报错了:" + e.getMessage());
        }
        return -1;
    }

    /**
     *
     * @param position
     * @return
     */
    public String getNavigationValue(int position) {
        T data = getItem(position);
        if (data != null && data instanceof INavigationMsg) {
            return ((INavigationMsg) data).getNavigationValue();
        }
        return null;
    }

    @Override
    public void setItemFirstId(Integer itemFirstViewId) {
        this.mItemFirstViewId = itemFirstViewId;
    }

    @Override
    public Object[] getSections() {
        return null;
    }

}
